from selenium import selenium
import unittest, time, re

class PermUnConfTest(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*firefox", "http://127.0.0.1:8000/")
        self.selenium.start()
    
    def test_drop_down(self):
        sel = self.selenium
        sel.open("/ecuaciones/#")
        sel.click("//input[@name='regimen' and @value='libre']")
        sel.type("//div[@id='libre']/table/tbody/tr[1]/td[1]/div/input", "250")
        sel.type("id_k", "100")
        sel.type("//div[@id='libre']/table/tbody/tr[2]/td[1]/div/input", "500")
        sel.type("//div[@id='libre']/table/tbody/tr[2]/td[2]/div/input", "0.5")
        sel.type("id_h", "10")
        sel.click("//input[@value='Calcular' and @type='submit' and @onclick='javascript:calcAcL();']")
        for i in range(60):
            try:
                if "" != sel.get_value("//div[@id='libre']/table/tbody/tr[3]/td[2]/div/input"): break
            except: pass
            time.sleep(1)
        else: self.fail("time out")
        self.assertEqual(float(sel.get_value("//div[@id='libre']/table/tbody/tr[3]/td[2]/div/input")), 0.138)
        
    def test_hydraulic_conductivity(self):
        sel = self.selenium
        sel.open("/ecuaciones/#")
        sel.click("//input[@name='regimen' and @value='libre']")
        sel.type("//div[@id='libre']/table/tbody/tr[1]/td[1]/div/input", "250")
        sel.type("//div[@id='libre']/table/tbody/tr[3]/td[2]/div/input", "1")
        sel.type("//div[@id='libre']/table/tbody/tr[2]/td[1]/div/input", "500")
        sel.type("//div[@id='libre']/table/tbody/tr[2]/td[2]/div/input", "0.5")
        sel.type("id_h", "10")
        sel.click("//input[@value='Calcular' and @type='submit' and @onclick='javascript:calcAcL();']")
        for i in range(60):
            try:
                if "" != sel.get_value("id_k"): break
            except: pass
            time.sleep(1)
        else: self.fail("time out")
        self.assertEqual(float(sel.get_value("id_k")), 14.466)
    
    def test_observation_radius(self):
        sel = self.selenium
        sel.open("/ecuaciones/#")
        sel.click("//input[@name='regimen' and @value='libre']")
        sel.type("//div[@id='libre']/table/tbody/tr[1]/td[1]/div/input", "250")
        sel.type("//div[@id='libre']/table/tbody/tr[3]/td[2]/div/input", "1")
        sel.type("//div[@id='libre']/table/tbody/tr[2]/td[1]/div/input", "500")
        sel.type("id_k", "10")
        sel.type("id_h", "10")
        sel.click("//input[@value='Calcular' and @type='submit' and @onclick='javascript:calcAcL();']")
        for i in range(60):
            try:
                if "" != sel.get_value("//div[@id='libre']/table/tbody/tr[2]/td[2]/div/input"): break
            except: pass
            time.sleep(1)
        else: self.fail("time out")
        self.assertEqual(float(sel.get_value("//div[@id='libre']/table/tbody/tr[2]/td[2]/div/input")), 4.218)
        
    def test_flow_rate(self):
        sel = self.selenium
        sel.open("/ecuaciones/#")
        sel.click("//input[@name='regimen' and @value='libre']")
        sel.type("//div[@id='libre']/table/tbody/tr[2]/td[2]/div/input", "0.5")
        sel.type("//div[@id='libre']/table/tbody/tr[3]/td[2]/div/input", "1")
        sel.type("//div[@id='libre']/table/tbody/tr[2]/td[1]/div/input", "500")
        sel.type("id_k", "10")
        sel.type("id_h", "10")
        sel.click("//input[@value='Calcular' and @type='submit' and @onclick='javascript:calcAcL();']")
        for i in range(60):
            try:
                if "" != sel.get_value("//div[@id='libre']/table/tbody/tr[1]/td[1]/div/input"): break
            except: pass
            time.sleep(1)
        else: self.fail("time out")
        self.assertEqual(float(sel.get_value("//div[@id='libre']/table/tbody/tr[1]/td[1]/div/input")), 172.821)
     
    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
